3 Lecture
CS402
Midterm & Final Term Short Notes
Regular Expression
A regular expression, or regex for short, is a sequence of characters that defines a search pattern. It is a powerful tool for text processing, used to match and manipulate strings of text based on certain rules or patterns. Regular expressions
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a regular expression? a) A sequence of numbers b) A sequence of characters that defines a search pattern c) A mathematical function d) None of the above Answer: b Which programming language uses regular expressions extensively? a) Java b) C++ c) Python d) All of the above Answer: d What is the basic syntax of a regular expression? a) ( ) b) { } c) [ ] d) / / Answer: d Which of the following characters is used to match any single character? a) * b) . c) + d) ? Answer: b What is the purpose of the pipe symbol (|) in a regular expression? a) To match the beginning of a line b) To match the end of a line c) To match either one pattern or another d) To match any character Answer: c Which quantifier is used to match zero or one occurrence of the preceding character? a) * b) + c) ? d) { Answer: c Which of the following regular expressions matches any digit? a) \w b) \d c) \s d) \W Answer: b Which character class matches any whitespace character? a) \w b) \d c) \s d) \W Answer: c Which of the following regular expressions matches the end of a line? a) $ b) ^ c) | d) . Answer: a What is the purpose of the lookahead assertion in a regular expression? a) To match a pattern only if it is followed by another pattern b) To match a pattern only if it is not followed by another pattern c) To match a pattern at the beginning of a line d) None of the above Answer: a
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a regular expression and what is it used for? Answer: A regular expression is a sequence of characters that defines a search pattern. It is used to match and manipulate strings of text based on certain rules or patterns. What is the difference between a basic and extended regular expression? Answer: Basic regular expressions use fewer metacharacters and have simpler syntax compared to extended regular expressions, which provide more functionality and are more expressive. What is the purpose of the dot (.) character in a regular expression? Answer: The dot character matches any single character in a regular expression. How is alternation expressed in a regular expression? Answer: Alternation is expressed in a regular expression using the pipe (|) symbol, which indicates a choice between two or more patterns. What is a character class in a regular expression? Answer: A character class in a regular expression is a set of characters that match any one character from the set. It is represented by enclosing the set in square brackets. What is the purpose of the caret (^) character in a regular expression? Answer: The caret character is used to match the beginning of a line in a regular expression. What is the difference between a greedy and non-greedy quantifier in a regular expression? Answer: A greedy quantifier matches as many characters as possible, while a non-greedy quantifier matches as few characters as possible. What is a backreference in a regular expression? Answer: A backreference is a reference to a previously matched group in a regular expression. What is the purpose of the lookahead assertion in a regular expression? Answer: The lookahead assertion is used to match a pattern only if it is followed by another pattern. How can regular expressions be used for input validation in web development? Answer: Regular expressions can be used to ensure that user input on a website conforms to a specific format, such as a valid email address or phone number.